#include "core/ljc.hpp"

namespace FastMD
{
	LJCutOff:: ~LJCutOff() throw() {}
	
	LJCutOff:: LJCutOff( const Real eps, const Real sig, const Real cut ) throw() :
	LJ( eps, sig ),
	r_c( cut ),
	r_c_sq( r_c * r_c ),
	V_c(0),
	F_c(0)
	{
		V3D       F;
		const V3D r(r_c,0,0);
		
		(Real&)V_c = LJ::operator()(F,r);
		(Real&)F_c = F.x;
		//std::cerr << "V_c(" << r_c <<")=" << V_c << std::endl;
	}
	
	Real LJCutOff:: operator()( V3D &f, const V3D &r ) const throw()
	{
		const Real r2 = r.normsq();
		if( r2 < r_c_sq )
		{
			const Real         q2     = sigma_sq / r2; 
			const Real         q6     =  q2 * q2 * q2;
			const Real         q12    =  q6 * q6;
			const Real         r1     = __sqrt(r2);
			
			f.mulset( _24epsilon * ( (q12+q12) - q6 ) / r2 - F_c/r1, r);
			return ( ( _4epsilon * ( q12 -  q6 ) ) - V_c ) + (r1-r_c) * F_c;
		}
		else
		{
			f.ldz();
			return 0;
		}
	}
	
	const char *LJCutOff:: name() const throw()
	{
		return "LennardJonesCutOff";
	}
	
}
